<template>
	<view class="uni-container">
		<uni-forms ref="form" :model="formData" err-show-type="toast" :label-width="100">
			<uni-forms-item name="plat_no" label="第三方平台">
				<picker mode="selector" :range-key="'label'" :range="thirdPlatPicker.columns" @change="platChange"
					@cancel="platCancel">
					<u-input disabled :value="platName"></u-input>
				</picker>
			</uni-forms-item>
			<uni-forms-item name="order_id" label="已核销订单号">
				<uni-easyinput placeholder="请输入订单号" v-model="formData.order_id"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="amount" label="订单实付金额">
				<uni-easyinput :clearable="clearable" placeholder="最大保留两位小数" type="digit" v-model="formData.amount">
					<template #right><text style="margin-right: 24rpx;">元</text></template>
				</uni-easyinput>
			</uni-forms-item>
		</uni-forms>
		<view class="uni-button-group">
			<button type="primary" class="uni-button" style="background-color: #0e100e;" @click="submit">提交</button>
		</view>
	</view>
</template>

<script>
	const app = getApp()
	export default {
		data() {
			return {
				clearable: false,
				formData: {
					operateType: app.globalData.crud.add,
					plat_no: 1,
					order_id: '',
					amount: null,
					business_id: null,
					order_amount: null,
				},
				thirdPlatPicker: {
					show: true,
					columns: [{
						label: '抖音',
						id: 1
					}, {
						label: '美团',
						id: 2
					}, {
						label: '大众点评',
						id: 3
					}, {
						label: '其他',
						id: 99
					}],
				},
				platName: ''
			}
		},
		onLoad() {
			this.platName = '抖音'
		},
		methods: {
			submit() {
				console.log('参数校验');
				if (this.formData.order_id === null || this.formData.order_id === '') {
					uni.showToast({
						title: '请输入订单号',
						icon: 'none'
					})
					return false
				}
				if (!this.validateMoney(this.formData.amount)) {
					uni.showToast({
						title: '金额格式不正确',
						icon: 'none'
					})
					return false
				}
				uni.showLoading({
					title: ''
				})
				// 处理其他参数
				this.formData.business_id = uni.getStorageSync(app.globalData.session.businessInfo).offlineBusinessId
				this.formData.order_amount = this.$mytools.regYuanToFen(this.formData.amount, 100)
				console.log(JSON.stringify(this.formData));
				// 新增
				let that = this
				uniCloud.callFunction({
					name: 'zhy-business-others-plat-order',
					data: this.formData,
					success(res) {
						if (res.result.code === 1) {
							// 成功
							that.$mytools.showSuccessToast()
						} else {
							// 失败
							that.$mytools.showErrorToast(res.result.msg)
						}
					},
					fail(err) {
						that.$mytools.showErrorToast('服务器内部错误，请联系管理员')
					},
					complete() {
						uni.hideLoading()
					}
				})
			},
			platChange(e) {
				console.log('platChange');
				this.formData.plat_no = this.thirdPlatPicker.columns[e.target.value].id
				this.platName = this.thirdPlatPicker.columns[e.target.value].label
				console.log(this.formData.plat_no);
			},
			platCancel(e) {},
			validateMoney(value) {
				return /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/.test(value)
			},
		}
	}
</script>

<style>
	page {
		background-color: rgba(242, 243, 248, 1);
	}

	.uni-container {
		padding-left: 5%;
		padding-right: 5%;
		padding-top: 5%;
		padding-bottom: 5%;
		width: 80%;
		margin-left: 5%;
		border-radius: 40rpx;
		margin-top: 20rpx;
		background-color: white;
	}

	.uni-button-group {
		margin-top: 10px;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		justify-content: center;
	}

	.uni-button {
		width: 184px;
	}
</style>